home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / libsrc~1.z / libsrc~1 / fdopen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-28  |  1.1 KB  |  64 lines

  1. /* from Dale Schumacher's dLibs */
  2.  
  3. #include <stdio.h>
  4. #include <sys/types.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7.  
  8. #define _NFILE NFILES
  9.  
  10. void _getbuf(FILE *);
  11. extern    FILE    _iob[];
  12.  
  13. FILE *fdopen(h, mode)
  14. register int h;
  15. _CONST register char *mode;
  16. {
  17.     register int i;
  18.     register unsigned int  f = 0;
  19.     register FILE *fp = NULL;
  20.     void _getbuf(FILE *);
  21.     
  22.     for(i=0; (!fp && (i < _NFILE)); ++i)
  23.     if(!(_iob[i]._flag & (_IORW | _IOREAD | _IOWRT)))
  24.         fp = &_iob[i];    /* empty slot */
  25.     if(!fp)
  26.     return(NULL);
  27.     while(*mode)
  28.     {
  29.     switch(*mode++)
  30.     {
  31.       case 'r':
  32.         f |= _IOREAD;
  33.         break;
  34.       case 'w':
  35.         f |= _IOWRT;
  36.         break;
  37.       case 'a':
  38.         f |= _IOWRT;
  39.         break;
  40.       case '+':
  41.         f &= ~(_IOREAD | _IOWRT);
  42.         f |= _IORW;
  43.         break;
  44.       case 'b':
  45.         break;
  46.       case 't':
  47.         break;
  48.       default:
  49. /*        fputs("Illegal file mode.\n", stderr); */
  50.         return(NULL);
  51.     }
  52.     }
  53.     if((i = (f & (_IORW | _IOREAD | _IOWRT))) == 0)
  54.     return(NULL);
  55.     if(isatty(h))
  56.     f |= (_IODEV | _IONBF);
  57.     else
  58.     f |= _IOFBF;
  59.     fp->_file = h;            /* file handle */
  60.     fp->_flag = f;            /* file status flags */
  61.     _getbuf(fp);
  62.     return(fp);
  63. }
  64.